<?php
/**
 * Controller is the customized base controller class.
 * All controller classes for this application should extend from this base class.
 */
class Controller extends CController
{
	/**
	 * @var string the default layout for the controller view. Defaults to '//layouts/column1',
	 * meaning using a single column layout. See 'protected/views/layouts/column1.php'.
	 */
	Const APPLICATION_ID = 'ASCCPE';
	public $layout='//layouts/column1';
	/**
	 * @var array context menu items. This property will be assigned to {@link CMenu::items}.
	 */
	public $menu=array();
	/**
	 * @var array the breadcrumbs of the current page. The value of this property will
	 * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
	 * for more details on how to specify this property.
	 */
	public $breadcrumbs=array();

	protected function checkRoleBase(){
		return true;
	}
	protected function _checkAuth()
    {
        // Check if we have the USERNAME and PASSWORD HTTP headers set?
    	if(!(isset($_SERVER['HTTP_X_'.self::APPLICATION_ID.'_USERNAME']) and isset($_SERVER['HTTP_X_'.self::APPLICATION_ID.'_PASSWORD']))) {
            // Error: Unauthorized
    		$this->_sendResponse(401);
    	}
    	$username = $_SERVER['HTTP_X_'.self::APPLICATION_ID.'_USERNAME'];
    	$password = $_SERVER['HTTP_X_'.self::APPLICATION_ID.'_PASSWORD'];
        // Find the user
    	$user=UserApp::model()->find('LOWER(userapp)=?',array(strtolower($username)));
        // print_r($user->password);     
    	if($user===null) {
            // Error: Unauthorized
    		$this->_sendResponse(401, 'Error: User Name is invalid');
        // } else if(!$user->validatePassword($password)) {
    	} else if($user->password !== $password) {
            // Error: Unauthorized
    		$this->_sendResponse(401, 'Error: User Password is invalid');
    	}
    } // }}} 
}